//
//  GGFWNet2.m
//  上海杨浦
//
//  Created by FrankChen on 13-5-27.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "GGFWNet2.h"

@implementation GGFWNet2


-(NSString *) getHtml:(NSString *)webUrl{
    
    NSURL *url = [NSURL URLWithString:webUrl];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSString *retStr = [[[NSString alloc]init]autorelease];
    retStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if([retStr isEqual:@""]){
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
    }
    
    return retStr;
}

-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}


-(NSString *) getData:(NSString *)html{
    NSString *content=[[[NSString alloc]init]autorelease];

    
    NSString *tag1=[[[NSString alloc]init]autorelease];
    tag1=@"<td>&nbsp;</td>";
    NSString *tag2=[[[NSString alloc]init]autorelease];
    tag2=@"</td>";
    
    NSString *tag31=[[[NSString alloc]init]autorelease];
    tag31=@"text-indent:0;}";
    NSString *tag32=[[[NSString alloc]init]autorelease];
    tag32=@"<table width=\"90%\"";
    NSString *tag4=[[[NSString alloc]init]autorelease];
    tag4=@"</table></td>";
    
    NSRange range1=[html rangeOfString:tag1];
    content=[html substringFromIndex:(range1.location+range1.length)];
    
    NSRange range2=[content rangeOfString:tag2];
    content=[content substringToIndex:(range2.location)];
    
    
    NSRange range31=[content rangeOfString:tag31];
    if(range31.length!=0){
        content=[content substringFromIndex:(range31.location+range31.length)];
    }else{
        NSRange range32=[content rangeOfString:tag32];
        if (range32.length!=0) {
            content=[content substringFromIndex:(range32.location)];
        }
        
    }
    

    NSRange range4=[content rangeOfString:tag4];
    if (range4.length!=0) {
        content=[content substringToIndex:(range4.location)];
    }
    
    
    content=[self filterHtmlTag:content];
    
    content=[self filterKongGe:content];
    
    content=[content stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    
    content=[content stringByAppendingString:@"\n                         "];
    //content=[content stringByAppendingString:tempcontent];
    
    return content;
    
}

-(NSString *)filterKongGe:(NSString *)content{
    NSRange range5;
    NSRange range6;
    NSRange range7;
    Boolean stop=false;
    while (!stop) {
        range7=[content rangeOfString:@"\r"];
        range5=[content rangeOfString:@" "];
        range6=[content rangeOfString:@"\n"];
        if(range5.location!=0&&range6.location!=0&&range7.location!=0){
            stop=true;
        }
        else{
            content=[content substringFromIndex:1];
        }
    }
    return content;
}

- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSString *result = [[[NSString alloc]init]autorelease];
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
        //        NSLog(@"start-> %d   end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
        //        NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
        // NSLog(@"Result--->%@", result);
        return [self filterHtmlTag:result];    //递归，过滤下一个标签
    }else{
        //result = [originHtmlStr stringByReplacingOccurrencesOfString:@"&nbsp" withString:@""];  // 过滤&nbsp等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
        result = [originHtmlStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    }
    return result;
}


@end
